package com.itcv.mybatis.demo.reflect;

import org.apache.ibatis.io.Resources;

import java.util.HashMap;
import java.util.Map;

public class TestProxy {

    static Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<>();

    public static void main(String[] args) {

        Class<?> boundType = null;
        String namespace = "com.itcv.mybatis.demo.reflect.HelloMapper";
        try {
            boundType = Resources.classForName(namespace);
            knownMappers.put(boundType,new MapperProxyFactory<>(boundType));
           HelloMapper helloMapper = getMapper(HelloMapper.class);
            System.out.println("======"+helloMapper.say("李明"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        // knownMappers.put()
    }

    public static  <T> T getMapper(Class<T> type) {
        final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
       return mapperProxyFactory.newInstance();
    }
}
